package main

import (
	"encoding/json"
	"net"
	"os/exec"
	"time"
)

func sendHeartbeat() {
	pk := map[string]interface{}{"type": "2"}
	msg, _ := json.Marshal(pk)
	for {
		conn, err := net.Dial("unix", "/var/run/guard/guard.sock")
		if err != nil {
			log.Error("connect to guard faild , Ready to restart")
			cmd := exec.Command("bash", "-c", "./guard.sh restart")
			err := cmd.Run()
			if nil != err {
				log.Error("restart faild, five seconds after retry %v", err)
			}
			time.Sleep(30 * time.Second)
			continue
		}
		for {
			err := SendData(conn, msg)
			if err != nil {
				break
			}
			time.Sleep(10 * time.Second)

		}
	}
}
